<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>学生管理首页</title>
  <style>
    .con {
      width: 1200px;
      margin: 0 auto;
      padding: 0;
    }

    .search {
      padding: 10px;
      width: 1200px;
      border: 1px solid skyblue;
    }

    .box {
      padding: 10px;
      width: 100%;

      border: 1px solid pink;
      text-align: center;
    }

    table {
      width: 100%;
    }

    tr {
      height: 40px;
    }

    table,
    tr,
    th,
    td {
      border: 1px solid gray;
      border-collapse: collapse;
    }
  </style>
</head>

<body>
  <div class="con">
    <div class="search">姓名：<input type="text" name="name"><button>搜索</button><a href="javascript:to()">添加</a></div>
    <div class="box">
      <table>
        <thead>
          <tr>
            <th>序号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>班级</th>
            <th>性别</th>
            <th>年龄</th>
            <th>电话</th>
            <th>爱好</th>
            <th>地址</th>
            <th>备注</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody></tbody>
      </table>
    </div>
  </div>
  <script>
    function getData(uname) {
      var xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function (e) {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
            var arr = JSON.parse(xhr.response)
            var str = ``
            arr.forEach(function (ele, index) {
              str += `
              <tr>
            <td>${index + 1}</td>
            <td>${ele.id}</td>
            <td>${ele.name}</td>
            <td>${ele.clazz}</td>
            <td>${ele.gender}</td>
            <td>${ele.age}</td>
            <td>${ele.tel}</td>
            <td>${ele.hobby}</td>
            <td>${ele.address}</td>
            <td>${ele.remark}</td>
            <td>${ele.date}</td>
            <td><a href="edit.html#${ele.id}">编辑</a>
              <a href="javascript:del(${ele.id})">删除</a>
              </td>
          </tr>
              `
            })
            document.querySelector('tbody').innerHTML = str
          }
        }

      }
      var url = 'http://localhost:3008/api/student/getstudent'
      if (uname) {
        url += '?name=' + uname
      }
      xhr.open('GET', url)
      xhr.send()
    }
    getData()

    var btn = document.querySelector('button')
    btn.addEventListener('click', function () {
      var input_Content = document.querySelector('input[name="name"]').value
      getData(input_Content)
    })


    function del(id) {
      var xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function (e) {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
            getData()
          }
        }

      }
      var url = 'http://localhost:3008/api/student/removestudent'
      xhr.open('POST', url)
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      xhr.send('id=' + id)
    }
    function to() {
      window.location.href = 'add.html'
    }
  </script>
</body>

</html>